<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Java 字符串反转的艺术</title>
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            min-height: 100vh;
        }
        .hero-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        .code-block {
            background: #1e1e1e;
            border-radius: 12px;
            overflow: hidden;
            box-shadow: 0 10px 30px rgba(0,0,0,0.2);
        }
        .code-header {
            background: #2d2d2d;
            padding: 10px 20px;
            display: flex;
            align-items: center;
            gap: 8px;
        }
        .code-dot {
            width: 12px;
            height: 12px;
            border-radius: 50%;
        }
        pre {
            margin: 0;
            padding: 20px;
            overflow-x: auto;
        }
        code {
            color: #d4d4d4;
            font-family: 'Consolas', 'Monaco', monospace;
            font-size: 14px;
            line-height: 1.6;
        }
        .method-card {
            transition: all 0.3s ease;
            border: 2px solid transparent;
        }
        .method-card:hover {
            transform: translateY(-5px);
            border-color: #667eea;
            box-shadow: 0 20px 40px rgba(102, 126, 234, 0.2);
        }
        .drop-cap {
            float: left;
            font-size: 4rem;
            line-height: 1;
            font-weight: 700;
            margin-right: 8px;
            color: #667eea;
            font-family: 'Noto Serif SC', serif;
        }
        .mermaid {
            display: flex;
            justify-content: center;
            margin: 2rem 0;
        }
        .feature-icon {
            width: 60px;
            height: 60px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            border-radius: 16px;
            display: flex;
            align-items: center;
            justify-content: center;
            color: white;
            font-size: 24px;
            margin-bottom: 1rem;
        }
    </style>
</head>
<body>
    <!-- Hero Section -->
    <section class="hero-gradient text-white py-20">
        <div class="container mx-auto px-6 text-center">
            <h1 class="text-5xl md:text-6xl font-bold mb-6 tracking-tight">
                Java 字符串反转的艺术
            </h1>
            <p class="text-xl md:text-2xl opacity-90 max-w-3xl mx-auto leading-relaxed">
                探索四种优雅的实现方式，从简单直接到算法精妙，掌握字符串操作的核心技巧
            </p>
            <div class="mt-10 flex justify-center gap-4">
                <span class="bg-white bg-opacity-20 px-4 py-2 rounded-full text-sm">
                    <i class="fas fa-code mr-2"></i>4 种实现方法
                </span>
                <span class="bg-white bg-opacity-20 px-4 py-2 rounded-full text-sm">
                    <i class="fas fa-clock mr-2"></i>5 分钟阅读
                </span>
            </div>
        </div>
    </section>

    <!-- Introduction -->
    <section class="container mx-auto px-6 py-16">
        <div class="max-w-4xl mx-auto bg-white rounded-2xl shadow-xl p-10">
            <p class="text-lg leading-relaxed text-gray-700">
                <span class="drop-cap">在</span>Java 编程中，字符串反转是一个经典且实用的操作。无论是面试题目还是实际开发，掌握多种实现方式都能让你游刃有余。本文将深入探讨四种不同的实现方法，每种方法都有其独特的优势和适用场景。
            </p>
        </div>
    </section>

    <!-- Visual Diagram -->
    <section class="container mx-auto px-6 py-8">
        <div class="max-w-6xl mx-auto bg-white rounded-2xl shadow-xl p-10">
            <h2 class="text-3xl font-bold text-center mb-8 text-gray-800">方法概览</h2>
            <div class="mermaid">
                graph TD
                    A[字符串反转方法] --> B[StringBuilder]
                    A --> C[字符数组]
                    A --> D[递归]
                    A --> E[栈]
                    
                    B --> B1[内置方法]
                    B --> B2[最简单高效]
                    
                    C --> C1[双指针交换]
                    C --> C2[原地操作]
                    
                    D --> D1[分治思想]
                    D --> D2[优雅实现]
                    
                    E --> E1[LIFO特性]
                    E --> E2[数据结构应用]
                    
                    style A fill:#667eea,stroke:#fff,stroke-width:3px,color:#fff
                    style B fill:#48bb78,stroke:#fff,stroke-width:2px,color:#fff
                    style C fill:#4299e1,stroke:#fff,stroke-width:2px,color:#fff
                    style D fill:#ed8936,stroke:#fff,stroke-width:2px,color:#fff
                    style E fill:#9f7aea,stroke:#fff,stroke-width:2px,color:#fff
            </div>
        </div>
    </section>

    <!-- Methods Section -->
    <section class="container mx-auto px-6 py-16">
        <div class="max-w-6xl mx-auto">
            <!-- Method 1: StringBuilder -->
            <div class="method-card bg-white rounded-2xl shadow-lg p-8 mb-8">
                <div class="flex items-start gap-6">
                    <div class="feature-icon">
                        <i class="fas fa-bolt"></i>
                    </div>
                    <div class="flex-1">
                        <h3 class="text-2xl font-bold mb-4 text-gray-800">1. 使用 StringBuilder</h3>
                        <p class="text-gray-600 mb-6 leading-relaxed">
                            StringBuilder 提供了内置的 <code class="bg-purple-100 text-purple-800 px-2 py-1 rounded">reverse()</code> 方法，这是最简单直接的实现方式。适合大多数场景，性能优秀且代码简洁。
                        </p>
                        <div class="code-block">
                            <div class="code-header">
                                <div class="code-dot bg-red-500"></div>
                                <div class="code-dot bg-yellow-500"></div>
                                <div class="code-dot bg-green-500"></div>
                                <span class="text-gray-400 text-sm ml-4">StringReverseDemo.java</span>
                            </div>
                            <pre><code>public class StringReverseDemo {
    public static void main(String[] args) {
        String original = "Hello, World!";
        String reversed = new StringBuilder(original).reverse().toString();
        System.out.println("反转后的字符串: " + reversed); // 输出: !dlroW ,olleH
    }
}</code></pre>
                        </div>
                    </div>
                </div>
            </div>

            <!-- Method 2: Character Array -->
            <div class="method-card bg-white rounded-2xl shadow-lg p-8 mb-8">
                <div class="flex items-start gap-6">
                    <div class="feature-icon">
                        <i class="fas fa-exchange-alt"></i>
                    </div>
                    <div class="flex-1">
                        <h3 class="text-2xl font-bold mb-4 text-gray-800">2. 手动反转字符数组</h3>
                        <p class="text-gray-600 mb-6 leading-relaxed">
                            使用双指针法在原地交换字符，空间复杂度为 O(1)。这种方法让你更深入理解字符串操作的底层原理，适合需要精细控制的场景。
                        </p>
                        <div class="code-block">
                            <div class="code-header">
                                <div class="code-dot bg-red-500"></div>
                                <div class="code-dot bg-yellow-500"></div>
                                <div class="code-dot bg-green-500"></div>
                                <span class="text-gray-400 text-sm ml-4">StringReverseDemo.java</span>
                            </div>
                            <pre><code>public class StringReverseDemo {
    public static void main(String[] args) {
        String original = "Hello, World!";
        char[] charArray = original.toCharArray();
        int left = 0;
        int right = charArray.length - 1;

        while (left < right) {
            char temp = charArray[left];
            charArray[left] = charArray[right];
            charArray[right] = temp;
            left++;
            right--;
        }

        String reversed = new String(charArray);
        System.out.println("反转后的字符串: " + reversed);
    }
}</code></pre>
                        </div>
                    </div>
                </div>
            </div>

            <!-- Method 3: Recursion -->
            <div class="method-card bg-white rounded-2xl shadow-lg p-8 mb-8">
                <div class="flex items-start gap-6">
                    <div class="feature-icon">
                        <i class="fas fa-sync-alt"></i>
                    </div>
                    <div class="flex-1">
                        <h3 class="text-2xl font-bold mb-4 text-gray-800">3. 使用递归</h3>
                        <p class="text-gray-600 mb-6 leading-relaxed">
                            递归方法体现了分治思想的优雅。虽然在处理长字符串时可能面临栈溢出风险，但它展示了问题分解的艺术，适合学习和理解递归思维。
                        </p>
                        <div class="code-block">
                            <div class="code-header">
                                <div class="code-dot bg-red-500"></div>
                                <div class="code-dot bg-yellow-500"></div>
                                <div class="code-dot bg-green-500"></div>
                                <span class="text-gray-400 text-sm ml-4">StringReverseDemo.java</span>
                            </div>
                            <pre><code>public class StringReverseDemo {
    public static void main(String[] args) {
        String original = "Hello, World!";
        String reversed = reverseRecursively(original);
        System.out.println("反转后的字符串: " + reversed);
    }

    public static String reverseRecursively(String str) {
        // 基础条件，字符串长度为 0 或 1 时直接返回
        if (str == null || str.length() <= 1) {
            return str;
        }
        // 递归调用，反转子串并将首字符追加到末尾
        return reverseRecursively(str.substring(1)) + str.charAt(0);
    }
}</code></pre>
                        </div>
                    </div>
                </div>
            </div>

            <!-- Method 4: Stack -->
            <div class="method-card bg-white rounded-2xl shadow-lg p-8 mb-8">
                <div class="flex items-start gap-6">
                    <div class="feature-icon">
                        <i class="fas fa-layer-group"></i>
                    </div>
                    <div class="flex-1">
                        <h3 class="text-2xl font-bold mb-4 text-gray-800">4. 使用栈</h3>
                        <p class="text-gray-600 mb-6 leading-relaxed">
                            利用栈的后进先出（LIFO）特性实现字符串反转。这种方法完美展示了数据结构在实际问题中的应用，帮助理解栈的工作原理。
                        </p>
                        <div class="code-block">
                            <div class="code-header">
                                <div class="code-dot bg-red-500"></div>
                                <div class="code-dot bg-yellow-500"></div>
                                <div class="code-dot bg-green-500"></div>
                                <span class="text-gray-400 text-sm ml-4">StringReverseDemo.java</span>
                            </div>
                            <pre><code>import java.util.Stack;

public class StringReverseDemo {
    public static void main(String[] args) {
        String original = "Hello, World!";
        Stack<Character> stack = new Stack<>();

        for (char c : original.toCharArray()) {
            stack.push(c);
        }

        StringBuilder reversed = new StringBuilder();
        while (!stack.isEmpty()) {
            reversed.append(stack.pop());
        }

        System.out.println("反转后的字符串: " + reversed.toString());
    }
}</code></pre>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </section>

    <!-- Summary Section -->
    <section class="container mx-auto px-6 py-16">
        <div class="max-w-4xl mx-auto bg-gradient-to-br from-purple-50 to-blue-50 rounded-2xl shadow-xl p-10">
            <h2 class="text-3xl